package com.kinon.market.fragment;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.kinon.market.R;
import com.kinon.market.view.GongzuorenyuanActivity;
import com.kinon.market.view.KefuActivity;
import com.kinon.market.view.LoginActivity;
import com.kinon.market.view.MainActivity;
import com.kinon.market.view.UrgentSetActivity;

/**
 * 设置
 */

public class ShezhiFragment extends Fragment implements OnClickListener {
    View iscatview = null;
    /**
     * 工作人员
     */
    private Button btn_gongzuorenyuan;
    /**
     * 客服电话
     */
    private Button btn_lianxikefu;
    private Button btn_shezhi;
    /**
     * 退出登录
     */
    private Button btn_tuichudenglu;
    private Button btn_urgentSet;
    private Context mContext;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mContext = getActivity();
        return inflater.inflate(R.layout.fragment_shezhi, null);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        initRes();
    }

    private void initRes() {
        btn_gongzuorenyuan = (Button) getView().findViewById(R.id.btn_gongzuorenyuan);
        btn_lianxikefu = (Button) getView().findViewById(R.id.btn_lianxikefu);
        btn_tuichudenglu = (Button) getView().findViewById(R.id.btn_tuichudenglu);
        btn_gongzuorenyuan = (Button) getView().findViewById(R.id.btn_gongzuorenyuan);
        btn_urgentSet = (Button) getView().findViewById(R.id.btn_urgentSet);

        btn_gongzuorenyuan.setOnClickListener(this);
        btn_lianxikefu.setOnClickListener(this);
        btn_tuichudenglu.setOnClickListener(this);
        btn_urgentSet.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_tuichudenglu:
                tuichudialog();
                break;
            case R.id.btn_gongzuorenyuan:
                Intent intent = new Intent(getActivity(), GongzuorenyuanActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_lianxikefu:
                Intent intentkefu = new Intent(getActivity(), KefuActivity.class);
                startActivity(intentkefu);
                break;
            case R.id.btn_urgentSet:
                Intent intentU = new Intent(getActivity(), UrgentSetActivity.class);
                startActivity(intentU);
                break;
        }
    }

    /**
     * 退出登录弹窗
     */
    private void tuichudialog() {

        final Dialog dialog = new Dialog(getActivity());
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

        iscatview = LayoutInflater.from(getActivity()).inflate(
                R.layout.dialog_pay, null);
        TextView tital = (TextView) iscatview.findViewById(R.id.tital);
        TextView tv_no = (TextView) iscatview.findViewById(R.id.tv_no);
        TextView tv_yes = (TextView) iscatview.findViewById(R.id.tv_yes);
        tital.setText("是否退出登录?");
        dialog.setContentView(iscatview);
        dialog.show();
        tv_no.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        tv_yes.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                SharedPreferences sp = getActivity().getSharedPreferences(
                        "spReg", Context.MODE_PRIVATE);
                Editor editor = sp.edit();
                editor.clear();
                editor.commit();
                dialog.dismiss();
                Intent logIntent = new Intent(mContext,
                        LoginActivity.class);
                startActivity(logIntent);
                MainActivity.context.finish();
//				getActivity().finish();

            }
        });
    }

}
